package com.campus.client.controller;


import com.campus.client.net.ClientConnector;
import com.campus.client.view.LoginFrame;
import com.campus.common.message.LoginResponse;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class LoginController extends Component {
    private LoginFrame loginFrame;
    private ClientConnector connector;

    public LoginController(LoginFrame loginFrame, ClientConnector connector) {
        this.loginFrame = loginFrame;
        this.connector = connector;
        setupEventHandlers();
    }

    private void setupEventHandlers() {
        // 设置登录按钮事件
        loginFrame.setLoginButtonAction(event -> {
            try {
                loginControl();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

        loginFrame.setRegisterButtonAction(event -> {
            try {
                registerControl();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public String loginControl() throws ClassNotFoundException, IOException {
        String username = loginFrame.getUsername();
        String password = loginFrame.getPassword();
        String role = loginFrame.getSelectedIdentity();
        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "用户名或密码不能为空！",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }
        LoginResponse resp = connector.login(username, password, role);

        if (resp.isSuccess()) {
            // 登录成功提示
            JOptionPane.showMessageDialog(this,
                    "登录成功！\n欢迎回来，" + username + "！",
                    "登录成功",
                    JOptionPane.INFORMATION_MESSAGE);

            // 登录成功后的操作，例如打开主界面等
            if(role == "管理员"){
                loginFrame.showAdminMainFrame();
            }
            else if(role == "学生"){
                loginFrame.showMainFrame();
            }
            else{
                loginFrame.showTeacherMainFrame();
            }
            // openMainApplication();
        } else {
            // 登录失败提示 - 凭据错误
            JOptionPane.showMessageDialog(this,
                    "用户名或密码错误，请重试！",
                    "登录失败",
                    JOptionPane.ERROR_MESSAGE);

            // 清空密码框
            loginFrame.clearPasswordField();
        }
        return resp.getMessage();
    }

    public String registerControl() throws ClassNotFoundException, IOException {
        String username = loginFrame.getUsername();
        String password = loginFrame.getPassword();
        String role = loginFrame.getSelectedIdentity();
        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "用户名或密码不能为空！",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }
        if(loginFrame.getSelectedIdentity()=="学生") {
            loginFrame.showStudentLoginFrame();
        } else if (loginFrame.getSelectedIdentity() == "教师") {
            loginFrame.showTeacherLoginFrame();
        }else{
            JOptionPane.showMessageDialog(this,
                    "管理员账号只能由管理员账号注册，不能直接注册！",
                    "注册失败",
                    JOptionPane.ERROR_MESSAGE);

            // 清空密码框
            loginFrame.clearPasswordField();
        }

        return "None";
    }

    // 可选：结束会话时关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}

